import timeit


# 使用list的内建方法append
def t1():
    li = []
    for i in range(10000):
        li.append(i)


# 列表相加
def t2():
    li = []
    for i in range(10000):
        li = li + [i]


# 列表推导式
def t3():
    li = [i for i in range(10000)]


# 类型转换 iterable-->list
def t4():
    li = list(range(10000))


# 使用list内建方法insert
def t5():
    li = []
    for i in range(10000):
        li.insert(0, i)


timer1 = timeit.Timer("t1()", "from __main__ import t1")
timer2 = timeit.Timer("t2()", "from __main__ import t2")
timer3 = timeit.Timer("t3()", "from __main__ import t3")
timer4 = timeit.Timer("t4()", "from __main__ import t4")
timer5 = timeit.Timer("t5()", "from __main__ import t5")

print("append: {}".format(timer1.timeit(number=100)))
print("[]+[]: {}".format(timer2.timeit(number=100)))
print("[i for]: {}".format(timer3.timeit(number=100)))
print("list(): {}".format(timer4.timeit(number=100)))
print("insert: {}".format(timer5.timeit(number=100)))

# 速度从快到慢:  可迭代对象强转列表类型, 列表推导式, 列表的append方法, 列表的insert方法, 列表相加


